package com.aso114.project.mvp.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.adapter.FragmentAdapter;
import com.aso114.project.base.BaseSimpleActivity;
import com.aso114.project.bean.EvenBaseBean;
import com.aso114.project.commonview.NoSlideViewPager;
import com.aso114.project.exam.QuestionsFragment;
import com.aso114.project.mvp.activity.fragment.CollectLeftFragment;
import com.aso114.project.util.Constant;
import com.aso114.project.util.SystemBarHelper;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的收藏
 */
public class MyCollectActivity extends BaseSimpleActivity {
    @BindView(R.id.layout_title_bar_back_iv)
    ImageView layoutTitleBarBackIv;
    @BindView(R.id.layout_title_bar_title_tv)
    TextView layoutTitleBarTitleTv;
    @BindView(R.id.layout_title_bar_right_iv)
    ImageView layoutTitleBarRightIv;
    @BindView(R.id.layout_title_bar_right_tv)
    TextView layoutTitleBarRightTv;
    @BindView(R.id.title_top_ly)
    RelativeLayout titleTopLy;
    @BindView(R.id.my_collect_tab)
    TabLayout myCollectTab;
    @BindView(R.id.my_collect_viewPager)
    NoSlideViewPager myCollectViewPager;
    private int position;

    @Override
    protected int getResouseId() {
        return R.layout.mycollect;
    }

    @Override
    protected void initView() {
        SystemBarHelper.immersiveStatusBar(this);
        SystemBarHelper.setHeightAndPadding(this, titleTopLy);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            SystemBarHelper.immersiveStatusBar(this, 0);
            SystemBarHelper.setStatusBarDarkMode(this);
        }
        position = getIntent().getIntExtra("position", 0);
        layoutTitleBarTitleTv.setText("我的收藏");
        struct();
        MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
        myCollectViewPager.setAdapter(adapter);
        myCollectTab.setupWithViewPager(myCollectViewPager);
        myCollectTab.setTabsFromPagerAdapter(adapter);
        myCollectViewPager.setPagingEnabled(false);//是否可左右滑动

        for (int i = 0; i < myCollectTab.getTabCount(); i++) {
            TabLayout.Tab tab = myCollectTab.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(getTabView(i));
            }

//            if(i==0){
//                updateTabTextView(myCollectTab.getTabAt(0), true);
//
//            }else{
//            }
            updateTabTextView(tab, false);
        }
        myCollectTab.getTabAt(position).select();
        updateTabTextView(myCollectTab.getTabAt(position), true);

        myCollectTab.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //tab被选的时候回调
                updateTabTextView(tab, true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //tab未被选择的时候回调
                updateTabTextView(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                //tab重新选择的时候回调
            }
        });
    }

    private View getTabView(int currentPosition) {
        View view = LayoutInflater.from(this).inflate(R.layout.tab_item, null);
        TextView textView = view.findViewById(R.id.tab_item_textview);
        textView.setText(Constant.titleType[currentPosition]);
        return view;
    }

    private void updateTabTextView(TabLayout.Tab tab, boolean isSelect) {

        if (isSelect) {
            //选中加粗
            TextView tabSelect = tab.getCustomView().findViewById(R.id.tab_item_textview);
            tabSelect.setTextColor(getResources().getColor(R.color.redcolor));
            tabSelect.setTextSize(18);
            tabSelect.setText(tab.getText());
        } else {
            TextView tabUnSelect = tab.getCustomView().findViewById(R.id.tab_item_textview);
            tabUnSelect.setTextSize(15);
            tabUnSelect.setTextColor(getResources().getColor(R.color.black333));
            tabUnSelect.setText(tab.getText());
        }
    }

    public static void struct() {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads().detectDiskWrites().detectNetwork()
                .penaltyLog().build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects() // 探测SQLite数据库操作
                .penaltyLog() // 打印logcat
                .penaltyDeath().build());
    }

    class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return CollectLeftFragment.newInstance(position);
        }

        @Override
        public int getCount() {
            return Constant.titleType.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return Constant.titleType[position];
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void getBroadcast(Context context, Intent intent) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.layout_title_bar_back_iv)
    public void onViewClicked() {
        finish();
    }

//    QuestionBean qdata;
//    @BindView(R.id.layout_title_bar_back_iv)
//    ImageView layoutTitleBarBackIv;
//    @BindView(R.id.layout_title_bar_title_tv)
//    TextView layoutTitleBarTitleTv;
//    @BindView(R.id.layout_title_bar_right_iv)
//    ImageView layoutTitleBarRightIv;
//    @BindView(R.id.layout_title_bar_right_tv)
//    TextView layoutTitleBarRightTv;
//    @BindView(R.id.my_collection_left)
//    TextView myCollectionLeft;
//    @BindView(R.id.my_collection_right)
//    TextView myCollectionRight;
//    @BindView(R.id.collect_fragment)
//    FrameLayout collectFragment;
//    @BindView(R.id.title_top_ly)
//    RelativeLayout titleTopLy;
//
//    private boolean isLeft = true;
//
//    CollectLeftFragment leftFragment;
//    CollectRightFragment rightFragment;
//
//    @Override
//    protected int getResouseId() {
//        return R.layout.mycollect;
//    }
//
//    @Override
//    protected void initView() {
//        SystemBarHelper.immersiveStatusBar(this);
//        SystemBarHelper.setHeightAndPadding(this, titleTopLy);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            SystemBarHelper.immersiveStatusBar(this, 0);
//            SystemBarHelper.setStatusBarDarkMode(this);
//        }
//        layoutTitleBarTitleTv.setText("我的收藏");
//        isLeft = getIntent().getBooleanExtra("isLeft", true);
//        struct();
//        setView();
//    }
//
//    public static void struct() {
//        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
//                .detectDiskReads().detectDiskWrites().detectNetwork()
//                .penaltyLog().build());
//        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
//                .detectLeakedSqlLiteObjects() // 探测SQLite数据库操作
//                .penaltyLog() // 打印logcat
//                .penaltyDeath().build());
//    }
//
//    @Override
//    protected void initData() {
//
//    }
//
//    @Override
//    protected void getBroadcast(Context context, Intent intent) {
//
//    }
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        // TODO: add setContentView(...) invocation
//        ButterKnife.bind(this);
//    }
//
//    @OnClick({R.id.layout_title_bar_back_iv, R.id.my_collection_left, R.id.my_collection_right})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.layout_title_bar_back_iv:
//                finish();
//                break;
//            case R.id.my_collection_left:
//                if (isLeft)
//                    return;
//                isLeft = true;
//                setView();
//                break;
//            case R.id.my_collection_right:
//                if (!isLeft) {
//                    return;
//                }
//                isLeft = false;
//                setView();
//                break;
//        }
//    }
//
//    public void setView() {
//        myCollectionLeft.setTextColor(getResources().getColor(R.color.a333333));
//        myCollectionRight.setTextColor(getResources().getColor(R.color.a333333));
//        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//        if (isLeft) {
//            if (leftFragment == null) {
//                leftFragment = new CollectLeftFragment();
//                transaction.add(R.id.collect_fragment, leftFragment);
//            }
//            myCollectionLeft.setTextColor(getResources().getColor(R.color.aff6559));
//            initFragment(transaction, leftFragment);
//        } else {
//            if (rightFragment == null) {
//                rightFragment = new CollectRightFragment();
//                transaction.add(R.id.collect_fragment, rightFragment);
//            }
//            myCollectionRight.setTextColor(getResources().getColor(R.color.aff6559));
//            initFragment(transaction, rightFragment);
//        }
//    }
//
//    public void initFragment(FragmentTransaction transaction, BaseFragment currentFragment) {
//        //隐藏所有fragment
//        hideFragment(transaction);
//        //显示需要显示的fragment
//        transaction.show(currentFragment);
//        //提交事务
//        transaction.commit();
//    }
//
//    public void hideFragment(FragmentTransaction transaction) {
//        if (leftFragment != null) {
//            transaction.hide(leftFragment);
//        }
//        if (rightFragment != null) {
//            transaction.hide(rightFragment);
//        }
//    }
}
